package classes
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	[Bindable]
	public class Settings extends EventDispatcher{
	
		private static var singleton : Settings
		
		private var _uploadServletURL:String;
		private var _showAllServletURL:String;
		private var _getTagsServletURL:String;
		private var _taggingServletURL:String;
		private var _adminTaskServletURL:String;
		private var _removeImageServletURL:String;
		
		[Bindable("settingsChanged")]
		public function get uploadServletURL():String{
			return _uploadServletURL;
		}
		
		[Bindable("settingsChanged")]
		public function get showAllServletURL():String{
			return _showAllServletURL;
		}
		[Bindable("settingsChanged")]
		public function get getTagsServletURL():String{
			return _getTagsServletURL;
		}
		[Bindable("settingsChanged")]
		public function get taggingServletURL():String{
			return _taggingServletURL;
		}
		[Bindable("settingsChanged")]
		public function get adminTaskServletURL():String{
			return _adminTaskServletURL;
		}
		[Bindable("settingsChanged")]
		public function get removeImageServletURL():String{
			return _removeImageServletURL;
		}
		
		public static function getInstance():Settings{
			if ( singleton == null )
				singleton = new Settings( arguments.callee );
			return singleton;
		}
		
		public function Settings( caller : Function = null ){ 
			if( caller != Settings.getInstance )
			throw new Error ("Singleton is a singleton class, use getInstance() instead");
			
			if ( Settings.singleton != null )
			throw new Error( "Only one Singleton instance should be instantiated" ); 
		}
		
		public function load():void{
			var httpService:HTTPService = new HTTPService();
			httpService.method = "GET";
			httpService.url = "settings/settings.xml";
			httpService.addEventListener(ResultEvent.RESULT, settingsLoaded);
			httpService.send();
		}
	
		private function settingsLoaded(event:ResultEvent):void{
			_uploadServletURL = event.result.settings.uploadservleturl.toString();
			_showAllServletURL = event.result.settings.allimagesservleturl.toString();
			_adminTaskServletURL = event.result.settings.admintaskservleturl.toString();
			_getTagsServletURL = event.result.settings.gettagsservleturl.toString();
			_taggingServletURL = event.result.settings.taggingservleturl.toString();
			_removeImageServletURL = event.result.settings.removeimageservleturl.toString();
			dispatchEvent(new Event("settingsChanged"));
		}
	}
}